package com.jming.jike.controller.backend;

import com.jming.jike.common.PageResult;
import com.jming.jike.common.ResponseResult;
import com.jming.jike.common.ValidatorGroup.*;
import com.jming.jike.dto.backend.OrdersDTO;
import com.jming.jike.enums.OrdersStatusEnum;
import com.jming.jike.enums.PayMethodEnum;
import com.jming.jike.service.backend.OrdersService;
import com.jming.jike.vo.backend.OrdersStatusVO;
import com.jming.jike.vo.backend.OrdersVO;
import com.jming.jike.vo.backend.PayMethodVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2022/04/29
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单相关接口", value = "/order")
public class OrdersController {

    @Resource
    private OrdersService ordersService;

    @PostMapping("/pageQuery")
    @ApiOperation(value = "分页查询订单信息", notes = "分页查询订单信息", produces = "application/json", httpMethod = "POST")
    public ResponseResult<PageResult<OrdersVO>> pageQuery(@RequestBody @Validated(Query.class) OrdersDTO ordersDTO) {

        return ResponseResult.success(ordersService.pageQuery(ordersDTO));
    }

    @GetMapping("/ordersDetail/{id}")
    @ApiOperation(value = "查询详情信息", notes = "查询详情信息", produces = "application/json", httpMethod = "GET")
    public ResponseResult<OrdersVO> ordersDetail(@PathVariable("id") Long id) {

        return ResponseResult.success(ordersService.ordersDetail(id));
    }

    @PutMapping("/operOrders")
    @ApiOperation(value = "操作订单", notes = "操作订单", produces = "application/json", httpMethod = "PUT")
    public ResponseResult<Boolean> operOrders(@RequestBody @Validated(Update.class) OrdersDTO ordersDTO) {

        return ResponseResult.success(ordersService.operOrders(ordersDTO));
    }


    @GetMapping("/getOrdersStatusList")
    @ApiOperation(value = "订单状态列表", notes = "订单状态列表", produces = "application/json", httpMethod = "GET")
    public ResponseResult<List<OrdersStatusVO>> getOrdersStatusList() {

        return ResponseResult.success(Arrays.stream(OrdersStatusEnum.values()).map(ose -> {
            OrdersStatusVO osv = new OrdersStatusVO();
            osv.setOrdersStatusCode(ose.getCode());
            osv.setOrdersStatusDesc(ose.getDesc());
            return osv;
        }).collect(Collectors.toList()));
    }

    @GetMapping("/getPayMethodList")
    @ApiOperation(value = "支付方式列表", notes = "支付方式列表", produces = "application/json", httpMethod = "GET")
    public ResponseResult<List<PayMethodVO>> getPayMethodList() {

        return ResponseResult.success(Arrays.stream(PayMethodEnum.values()).map(pm -> {
            PayMethodVO pmv = new PayMethodVO();
            pmv.setPayMethodCode(pm.getCode());
            pmv.setPayMethodDesc(pm.getDesc());
            return pmv;
        }).collect(Collectors.toList()));
    }

}
